//
//  ScrollPicControl.m
//  OCUI
//
//  Created by login on 15-7-1.
//
//

#import "ScrollPicControl.h"
int ImageHeight=220;
@implementation ScrollPicControl

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;

    // Do any additional setup after loading the view, typically from a nib.
    self.arrayImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"image1.jpg"],[UIImage imageNamed:@"image2.jpg"],[UIImage imageNamed:@"image3.jpg"], nil];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,ImageHeight)];
    [_scrollView setPagingEnabled:YES];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [_scrollView setDelegate:self];
    [_scrollView setBackgroundColor:[UIColor lightGrayColor]];
    
    //ContentSize 这个属性对于UIScrollView挺关键的，取决于是否滚动。
    [_scrollView setContentSize:CGSizeMake(CGRectGetWidth(self.view.frame) * [self.arrayImages count], ImageHeight)];
    [self.view addSubview:_scrollView];
    
    
     _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(_scrollView.frame)-20, screenWidth, 20)];
    
    [_pageControl setBackgroundColor:[UIColor redColor]];
    
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = [self.arrayImages count];
    [_pageControl addTarget:self action:@selector(scrollPages) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    
    _viewController = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < [self.arrayImages count]; i++) {
        [_viewController addObject:[NSNull null]];
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];
      [self intViewPage];
}
-(void)intViewPage
{
   
    for(NSInteger i=0;i<[self.arrayImages count];i++)
    {
        
        UIViewController * imageViewController = [[UIViewController alloc] init];
        [self.viewController replaceObjectAtIndex:i withObject:imageViewController];
        if (imageViewController.view.superview == nil) {
            CGRect frame = self.scrollView.frame;
            frame.origin.x = CGRectGetWidth(frame) * i;
            frame.origin.y = 0;
            imageViewController.view.frame = frame;
            
            [self.scrollView addSubview:imageViewController.view];
            [imageViewController didMoveToParentViewController:self];
            UIImage * image=(UIImage *)[self.arrayImages objectAtIndex:i];
            [imageViewController.view setBackgroundColor:[UIColor colorWithPatternImage:image]];
            imageViewController.view.contentMode=UIViewContentModeRedraw;
        }
        
    }
}
-(void)loadScrollViewPage:(NSInteger)page
{
    if (page >= self.arrayImages.count) {
        return;
    }
    
    UIViewController *imageViewController = [self.viewController objectAtIndex:page];
    if ((NSNull *)imageViewController == [NSNull null])
    {
        imageViewController = [[UIViewController alloc] init];
        [self.viewController replaceObjectAtIndex:page withObject:imageViewController];
    }
    
    if (imageViewController.view.superview == nil) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = CGRectGetWidth(frame) * page;
        frame.origin.y = 0;
        imageViewController.view.frame = frame;
        
        [self.scrollView addSubview:imageViewController.view];
        [imageViewController didMoveToParentViewController:self];
        UIImage * image=(UIImage *)[self.arrayImages objectAtIndex:page];
        [imageViewController.view setBackgroundColor:[UIColor colorWithPatternImage:image]];
        imageViewController.view.contentMode=UIViewContentModeRedraw;
    }
}
-(void)scrollPages{
    ++self.pageControl.currentPage;
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    if (isFromStart) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        self.pageControl.currentPage = 0;
    }
    else
    {
        [self.scrollView setContentOffset:CGPointMake(pageWidth*self.pageControl.currentPage, self.scrollView.bounds.origin.y)];
        
    }
    if (_pageControl.currentPage == _pageControl.numberOfPages - 1) {
        isFromStart = YES;
    }
    else
    {
        isFromStart = NO;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
